package com.ly.cloud.service.impl;

import com.ly.cloud.dto.CodeRuleDetailDto;
import com.ly.cloud.entity.CodeRuleDetailPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.CodeRuleDetailMapper;
import com.ly.cloud.service.CodeRuleDetailService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.utils.IpUtils;
import com.ly.cloud.vo.CodeRuleDetailVo;
import com.ly.cloud.vo.CodeRuleOperateDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: pengshiqng
 * @Description:
 * @Date: Create in 2018/12/7
 */
@Service
public class CodeRuleDetailServiceImpl implements CodeRuleDetailService {

    @Autowired
    private CodeRuleDetailMapper codeRuleDetailPoMapper ;

    /**
     * 分页+条件查询
     * @param columnName
     * @return
     */
    public List<CodeRuleDetailVo> selectAll(String columnName,String templateId) {
        try {
            return codeRuleDetailPoMapper.selectAll(columnName,templateId);
        } catch (Exception e) {
            throw new CloudException("查询异常，请重试!", e);
        }
    }



    /**
     * 根据id删除
     * @param id
     * @return
     */
    public int deleteById(Long id) {
        try {
            return codeRuleDetailPoMapper.deleteById(id);
        } catch (Exception e) {
            throw new DBException("删除编码规则详情失败！", e);
        }
    }


    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    public int deleteByIds(List<Long> ids) {
        try {
            return codeRuleDetailPoMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            throw new DBException("批量删除编码规则详情失败！", e);
        }
    }

    @Override
    public List<CodeRuleOperateDetailVo> showPop(String templateId) {
        try {
           return codeRuleDetailPoMapper.showPop(templateId);
        } catch (Exception e) {
            throw new DBException("查询编码规则-编码规则操作详情失败！", e);
        }
    }

    @Override
    @Transactional
    public void insert(String loginUserName, CodeRuleDetailDto dto) {
        try {
            //把dto对象转化成po对象
            CodeRuleDetailPo po = BeanCopyUtils.copyBeanProperties(dto, CodeRuleDetailPo.class);
            po.setCjsj(new Date());
            po.setCjr(loginUserName);
            po.setIp(IpUtils.getIp());
            codeRuleDetailPoMapper.insert(po);
        } catch (Exception e) {
            throw new DBException("新增失败！", e);
        }
    }

    @Override
    public int update(String loginUserName, CodeRuleDetailDto dto) {
        try {
            //转换dto 为 po对象
            CodeRuleDetailPo po = BeanCopyUtils.copyBeanProperties(dto, CodeRuleDetailPo.class);
            //设置操作人、操作ip、时间
            po.setIp(IpUtils.getIp());
            po.setCjr(loginUserName);
            po.setCjsj(new Date());
            return codeRuleDetailPoMapper.updateById(po);
        } catch (Exception e) {
            throw new DBException("查询编码规则-编码规则操作详情失败！", e);
        }
    }


}
